package org.web.common.sso.server.controller;

import com.google.gson.Gson;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.web.base.domain.ResultDO;
import org.web.base.domain.exception.ResultMessageEnum;

import org.web.base.domain.helper.ResultHelper;
import org.web.base.domain.helper.ServiceExceptionHelper;
import org.web.common.sso.client.domain.SessionAccountDO;
import org.web.common.sso.server.biz.AbstractTokenBiz;
import org.web.common.sso.server.controller.info.SsoMessageInfoEnum;
import org.web.common.sso.server.domain.SsoTokenDO;
import org.web.common.sso.server.helper.SSOServerConstant;
import org.web.common.sso.server.helper.redis.CacheSessionDO;
import org.web.common.sso.server.query.QuerySsoToken;
import org.web.common.sso.server.service.SsoTokenService;


import javax.annotation.Resource;


@Controller
@RequestMapping("token")
public class TokenController implements SSOServerConstant {

    Logger logger = LoggerFactory.getLogger(TokenController.class);

    @Resource
    private SsoTokenService ssoTokenService;

    @Resource
    private AbstractTokenBiz tokenBiz;

    @RequestMapping(value = "getSessionByToken", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public String getSessionByToken(String token) {
        ResultDO<SessionAccountDO> resultDO = getSessionAccountDOByToken(token);
        return new Gson().toJson(resultDO);
    }

    @RequestMapping(value = "getSessionBySSOExchangeToken", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @SuppressWarnings("unchecked")
    public String getSessionBySSOExchangeToken(String ssoExchangeToken) {
        ResultDO<SessionAccountDO> resultDO = new ResultDO<>();
        try {
            if (StringUtils.isEmpty(ssoExchangeToken)) {
                throw ServiceExceptionHelper.buildServiceException(ResultMessageEnum.PARAM_EMPTY, SsoMessageInfoEnum.getSessionByToken_token_missing.getMessage());
            }
            QuerySsoToken querySsoToken = new QuerySsoToken();
            querySsoToken.setSsoExchangeToken(ssoExchangeToken);
            SsoTokenDO ssoTokenDO = ssoTokenService.selectOneSsoToken(querySsoToken);
            if (ssoTokenDO == null) {
                ResultHelper.buildResultDOByResultMessageEnum(resultDO, ResultMessageEnum.DATA_NOT_EXIST, SsoMessageInfoEnum.getSessionBySSOExchangeToken_info_missing.getMessage(), null);
            } else {
                resultDO = getSessionAccountDOByToken(ssoTokenDO.getSsoToken());
                if (resultDO.isSuccess()) {
                    resultDO.getDomain().setSsoToken(ssoTokenDO.getSsoToken());
                }
            }
        } catch (Exception e) {
            logger.error(ServiceExceptionHelper.getExceptionInfo(e));
            resultDO = ResultHelper.buildResultDOByException(e);
        }
        return new Gson().toJson(resultDO);
    }

    @SuppressWarnings("unchecked")
    private ResultDO<SessionAccountDO> getSessionAccountDOByToken(String token) {
        ResultDO<SessionAccountDO> resultDO = new ResultDO<>();
        // SSOServerConfig.inCashTime
        try {
            if (StringUtils.isEmpty(token)) {
                throw ServiceExceptionHelper.buildServiceException(ResultMessageEnum.PARAM_EMPTY, SsoMessageInfoEnum.getSessionByToken_token_missing.getMessage());
            }

            ResultDO<CacheSessionDO> getAndRefreshCacheSessionResultDO = tokenBiz.getAndRefreshCacheSessionDO(token);
            if (getAndRefreshCacheSessionResultDO.isSuccess()) {
                SessionAccountDO sessionAccountDO = new SessionAccountDO();
                sessionAccountDO.setAccountNum(getAndRefreshCacheSessionResultDO.getDomain().getAccountNum());
                sessionAccountDO.setAccountName(getAndRefreshCacheSessionResultDO.getDomain().getAccountName());
                resultDO.setDomain(sessionAccountDO);
            } else {
                resultDO.setSuccess(false);
                resultDO.setCode(getAndRefreshCacheSessionResultDO.getCode());
                resultDO.setMessage(getAndRefreshCacheSessionResultDO.getMessage());
            }
        } catch (Exception e) {
            logger.error(ServiceExceptionHelper.getExceptionInfo(e));
            resultDO = ResultHelper.buildResultDOByException(e);
        }
        return resultDO;
    }

}
